package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.product.infrastructure.service.CommodityBannerService;
import com.zmc.product.infrastructure.bean.query.CommodityBannerQuery;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityBannerDao;
import com.zmc.product.infrastructure.entity.CommodityBannerEntity;


/**
 * @author zmc
 */
@Service("commodityBannerService")
public class CommodityBannerServiceImpl extends ServiceImpl<CommodityBannerDao, CommodityBannerEntity> implements CommodityBannerService {


    @Override
    public PageUtils pageCommodityBannerList(CommodityBannerQuery query) {
        Page<CommodityBannerEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<CommodityBannerEntity> commodityBannerList(CommodityBannerQuery query) {
        return list(getWrapper(query));
    }

    @Override
    public List<CommodityBannerEntity> getListByType(String distributionSite) {
        LambdaQueryWrapper<CommodityBannerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityBannerEntity::getType, distributionSite);
        return list(wrapper);
    }


    private LambdaQueryWrapper<CommodityBannerEntity> getWrapper(CommodityBannerQuery query) {
        LambdaQueryWrapper<CommodityBannerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getType() != null, CommodityBannerEntity::getType, query.getType())
                .eq(StringUtils.isNotEmpty(query.getSkuId()), CommodityBannerEntity::getSkuId, query.getSkuId())
                .ge(StringUtils.isNotEmpty(query.getBeginTime()), CommodityBannerEntity::getCreateTime, query.getBeginTime())
                .le(StringUtils.isNotEmpty(query.getEndTime()), CommodityBannerEntity::getCreateTime, query.getEndTime())
                .orderByDesc(CommodityBannerEntity::getCreateTime);
        return wrapper;
    }
}